/// aggregate root
use std::rc::Rc;

pub struct SaveUser {
    user: Option<crate::domain::user::entity::User>,
    user_repository: Rc<Box<dyn crate::domain::user::repository::UserRepository>>,
}
impl SaveUser {
    pub fn new(
        user_repository: Rc<Box<dyn crate::domain::user::repository::UserRepository>>,
    ) -> Self {
        Self {
            user_repository,
            user: None,
        }
    }

    pub fn find_by_id(&mut self, id: String) {
        self.user = self
            .user_repository
            .find_by_id(id)
            .map_or_else(|| None, |user| Some(user));
    }
    pub fn exist(&self) -> bool {
        self.user.is_some()
    }
    pub fn save(&self) -> Result<(), String> {
        let u = self.user.as_ref().unwrap();
        self.user_repository.save(u.clone())
    }
}
